package com.example.yuejian23.yuejian_23.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.yuejian23.yuejian_23.Adapter.PeixunAdapter;
import com.example.yuejian23.yuejian_23.Adapter.ShaituAdapter;
import com.example.yuejian23.yuejian_23.Data.SpacesItemDecoration;
import com.example.yuejian23.yuejian_23.R;
import com.example.yuejian23.yuejian_23.javaBean.PeixunBean;
import com.example.yuejian23.yuejian_23.javaBean.ShaituBean;
import com.example.yuejian23.yuejian_23.service.PeixunService;
import com.example.yuejian23.yuejian_23.service.ShaituService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by dong on 2017/10/10.
 */

public class ShaituFragment extends Fragment {
    RecyclerView recyclerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout.tab02,container,false);
        recyclerView =(RecyclerView) view.findViewById(R.id.shaitu);
        //recyclerView.setLayoutManager(new LinearLayoutManager(ShaituFragment.this.getContext()));
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        //SpacesItemDecoration decoration=new SpacesItemDecoration(2);
        //recyclerView.addItemDecoration(decoration);
        showData();

        return view;
    }

    private void showData()
    {

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://115.159.93.244/new18yuejian/api.php/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ShaituService shaituService=retrofit.create(ShaituService.class);

        Call<List<ShaituBean>> call=shaituService.getList();

        call.enqueue(new Callback<List<ShaituBean>>() {
            @Override
            public void onResponse(Call<List<ShaituBean>> call, Response<List<ShaituBean>> response) {
                List<ShaituBean> shaitu=response.body();

                recyclerView.setAdapter(new ShaituAdapter(getActivity(),shaitu));

            }

            @Override
            public void onFailure(Call<List<ShaituBean>> call, Throwable t) {

            }
        });

    }
}